package com.read.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 字符串操作方法
 * @author ksir
 *
 */
public class StringUtils {
	private static final Logger log = LoggerFactory.getLogger(StringUtils.class);
	/**
	 * 将驼峰式字符串转换为下划线式
	 * @param str
	 * @return
	 */
	public static String toUnderlineName(String str){
		if(str == null || str.equals("")){
			return null;
		}
		Matcher m = Pattern.compile("[A-Z]").matcher(str);
		while(m.find()){
			if(str.charAt(0)==Character.toUpperCase(str.charAt(0))){
				str = str.replaceFirst("[A-Z]", m.group().toLowerCase());
			}else{
				str = str.replaceFirst("[A-Z]", "_"+m.group().toLowerCase());
			}
		}
		//log.debug("转换结果：[{}]",str);
		return str;
	}
	
	/**
	 * 将下划线式字符串转换为驼峰式
	 * @param str
	 * @return
	 */
	public static String toCamelCase(String str){
		if(str == null || str.equals("")){
			return null;
		}
		Matcher m = Pattern.compile("_[a-z]").matcher(str);
		while(m.find()){
			if(str.charAt(0)=='_'){
				str = str.replaceFirst("_", "");
			}else{
				str = str.replaceFirst("_[a-z]", m.group().toUpperCase());
			}
		}
		str = str.replaceAll("_","");
		return str;
	}
	
	
	
	public static String toString(String[] str){
		StringBuffer buf = new StringBuffer();
		for(String s:str){
			buf.append(s).append(",");
		}
		buf.deleteCharAt(buf.length()-1);
		return buf.toString();
	}
}
